Skip to content

[Repo Assist] fix: replace bare except clauses with specific NetworkX exceptions#1421

Open
github-actions[bot] wants to merge 1 commit intomainfrom
repo-assist/improve-bare-excepts-20260326-4d402371722f8b60
Open

[Repo Assist] fix: replace bare except clauses with specific NetworkX exceptions#1421
github-actions[bot] wants to merge 1 commit intomainfrom
repo-assist/improve-bare-excepts-20260326-4d402371722f8b60

Conversation

@github-actions
Copy link
Contributor

🤖 This is an automated pull request from Repo Assist.

Summary

Two bare except: clauses in the dowhy codebase were masking all exceptions — including MemoryError, KeyboardInterrupt, and genuine programming bugs. This PR replaces them with the precise NetworkX exceptions that are actually expected.

Changes

dowhy/utils/graph_operations.py (line 244)

Before:

try:
    path = shortest_path(u, pa, i)
    return pa
except:
    pass

After:

try:
    path = shortest_path(u, pa, i)
    return pa
except nx.NetworkXNoPath:
    pass

networkx.algorithms.shortest_paths.generic.shortest_path raises nx.NetworkXNoPath when no path exists between pa and i. This is the only expected "not found" scenario here. This fix is consistent with the identical pattern already used in dowhy/utils/networkx_plotting.py.

dowhy/causal_identifier/id_identifier.py (line 128)

Before:

try:
    tsort_node_names = OrderedSet(list(nx.topological_sort(graph)))
except:
    raise Exception("The graph must be a directed acyclic graph (DAG).")

After:

try:
    tsort_node_names = OrderedSet(list(nx.topological_sort(graph)))
except nx.NetworkXUnfeasible:
    raise Exception("The graph must be a directed acyclic graph (DAG).")

nx.topological_sort() raises nx.NetworkXUnfeasible when the graph contains a cycle (i.e., is not a DAG). That is precisely the condition the error message describes.

Rationale

Bare except: clauses:

  1. Silently swallow KeyboardInterrupt and SystemExit, making it impossible to interrupt the program
  2. Hide genuine bugs (e.g. a NameError inside shortest_path would be silently ignored)
  3. Are flagged by flake8 (E722) and considered an anti-pattern in Python

Both replacements are behaviour-preserving — the original intent is captured precisely by the specific exception types.

Test Status

⚠️ No Python environment with project dependencies was available in the runner for this run. The changes are pure exception-type refinements with no logic changes, and the fix in graph_operations.py mirrors the existing except nx.NetworkXNoPath: pattern already used in networkx_plotting.py. Tests will run via CI.

Generated by Repo Assist

Note

🔒 Integrity filtering filtered 38 items

Integrity filtering activated and filtered the following items during workflow execution.
This happens when a tool call accesses a resource that does not meet the required integrity or secrecy level of the workflow.

Generated by Repo Assist ·

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@b897c2f3e43bde9ff7923c8fa9211055b26e27cc

- graph_operations.py:244: catch nx.NetworkXNoPath instead of bare
  except, consistent with the same pattern in networkx_plotting.py
- id_identifier.py:128: catch nx.NetworkXUnfeasible instead of bare
  except, which is the exception nx.topological_sort() raises for
  graphs containing cycles

Bare except clauses mask unrelated bugs (e.g. MemoryError, KeyboardInterrupt)
and make debugging harder. Both replacements preserve existing behaviour exactly.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants